package com.carkeeper.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.dto.OrderDTO;
import com.carkeeper.service.OrderDetailService;
import com.carkeeper.service.OrderFeign;
import com.carkeeper.service.OrderLogFeign;
import com.carkeeper.service.OrderService;
import com.carkeeper.entity.OrderPO;
import com.carkeeper.entity.OrderDetailPO;
import com.carkeeper.vo.OrderVO;
import com.carkeeper.vo.Pager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private OrderLogFeign orderLogFeign;

    /*
     * 创建订单
     * */
    @PostMapping
    public String createOrder(@RequestBody OrderVO orderVo) {
        OrderPO order = new OrderPO();
        if (orderVo.getStoreId() != null) {
            order.setStoreId(orderVo.getStoreId());
        }
        if (orderVo.getUserId() != null) {
            order.setUserId(orderVo.getUserId());
        }
        if (orderVo.getCarCode() != null) {
            order.setCarCode(orderVo.getCarCode());
        }
        order.setType(orderVo.getType());
        String serviceDate = orderVo.getServiceDate();
        try {
            Date date = new SimpleDateFormat("yyyy-MM-dd").parse(serviceDate);
            order.setCreateTime(LocalDateTime.now());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            orderService.createOrder(order);
            OrderDetailPO orderDetailPO = new OrderDetailPO();
            orderDetailPO.setOrderId(order.getId());
            orderDetailPO.setCode(orderVo.getOrderDetailPO().getCode());
            orderDetailPO.setNum(orderVo.getOrderDetailPO().getNum());
            orderDetailService.createOrderDetailService(orderDetailPO);
            return "创建成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "创建失败";
        }
    }

    /*
     * 删除订单
     * */
    @DeleteMapping("/{id}")
    public String del(@PathVariable("id") Long id) {
        try {
            orderService.delById(id);
            return "删除成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败";
        }
    }

    /*
     * 更新订单信息
     * */
    @PutMapping
    public String update(@RequestBody OrderVO orderVo) {
        long id = Long.parseLong(String.valueOf(orderVo.getId()));

        OrderPO order = orderService.getOrder(id);
        if (orderVo.getCarCode() != null) {
            order.setCarCode(orderVo.getCarCode());
        }
        if (orderVo.getServiceDate() != null) {
            order.setServiceDate(orderVo.getServiceDate());
        }
        if (orderVo.getPayStatus() != null) {
            order.setPayStatus(orderVo.getPayStatus());
        }
        if (orderVo.getOrderStatus() != null) {
            order.setOrderStatus(orderVo.getOrderStatus());
        }
        try {
            orderService.update(order);
            return "更新成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "更新失败";
        }
    }

    /*
     *
     * 获取订单分页数据*/
    @GetMapping
    public Pager<OrderPO> getOrderPage(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pagesize,
                                       @RequestParam(name = "type", required = false) Integer type,
                                       @RequestParam(name = "contact", required = false) String contact,
                                       @RequestParam(name = "code", required = false) String code,
                                       @RequestParam(name = "orderStatus", required = false) String orderStatus,
                                       @RequestParam(name = "createTimeStart", required = false) String createTimeStart,
                                       @RequestParam(name = "createTimeEnd", required = false) String createTimeEnd,
                                       @RequestParam(name = "storeName", required = false) String storeName,
                                       @RequestParam(name = "payStatus",required = false)String payStatus) {
        Page<OrderPO> orderPage = orderService.getOrderPage(page, pagesize, type, contact, orderStatus, createTimeStart, createTimeEnd, storeName, code,payStatus);
        Pager<OrderPO> orderPOPager = new Pager<>(orderPage);
        return orderPOPager;
    }

    /*
     * 获取订单信息
     * */
    @GetMapping("/{id}")
    public OrderPO getOrder(@PathVariable("id") Long id) {
        try {
            OrderPO order = orderService.getOrder(id);
            return order;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/list")
    public List<OrderDTO> findAll(@RequestParam(name = "storeId", required = false) Long storeId,
                                  @RequestParam(name = "orderStatus", required = false) String orderStatus,
                                  @RequestParam(name = "payStatus", required = false) String payStatus) {
        return orderService.findAll(storeId, orderStatus, payStatus).stream().map(po -> {
            OrderDTO dto = new OrderDTO();
            BeanUtils.copyProperties(po, dto);
            return dto;
        }).collect(Collectors.toList());
    }
}

